/*!
 * Dashboard.
 * Date: 2015/07/06.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg) {
  $(function () {
    var 
      $dataFrm = $('#data-frm'),
      $submitBtn = $dataFrm.find('button[type="submit"]'),
      dataFieldRule, options, dataValidator;
    // console.log($dataFrm);
    dataFieldRule = {
      user_pwd:{
        rule:{
            required:{
              param:'password',
              message:VAM.j('Password cannot be empty')
            },
            password:{
                message:VAM.j('Password must be character、number')
            },
            minlength:{
                param:6,
                message:VAM.j('Minimum password length of 6 characters')
            },
            maxlength:{
                param:20,
                message:VAM.j('Password length up to 20 characters')
            },
            password1:{
                param:$('#confirm_pwd'),
                message: VAM.j('Please enter password')
            },
            password3:{
                param:$('#confirm_pwd'),
                message: VAM.j('Confirm password does not agree with the new password')
            }
        },
        messageContainer:'user_pwd_err',
        eventName:'change'
      },
      confirm_pwd:{
        rule:{
            required:{
                param:'password',
                message:VAM.j('Enter Confirm Password')
            },
            password2:{
                param:$('#user_pwd'),
                message:VAM.j('Confirm password does not agree with the new password')
            }
        },
        messageContainer:'confirm_pwd_err'
      },
      email_address: {
        rule: {
          required: {
            message: VAM.j('Email address cannot be empty')
          },
          email: {
            message: VAM.j('Please enter the correct email address')
          }
        },
        messageContainer: 'email_address_err'
      }
    };
    options = {
      submitOnSucceed: false,
      onvalidate: function(evt) {
        if (evt.resultList.length <= 0) {
            $dataFrm.ajaxSubmit({
                beforeSubmit: function() {
                  $submitBtn.button('loading');
                },
                success: function (data) {
                  $submitBtn.button('reset');
                  if (data.status === 'success') {
                    VamOpMsg.messager(data);
                    $('#back-btn').trigger('click', 'success');
                  } else {
                    VamOpMsg.messager(data);
                  }
                }
            });
        };
      }
    };
    formValidator.validate($dataFrm, dataFieldRule, options);
    dataValidator = formValidator.dataValidator;
    dataValidator.addRule('password1', function(val, opt){
        opt.trigger('change');
        return true;
      });
    dataValidator.addRule('password3', function(val, opt){
      if (opt.val()) {
        return val===opt.val();
      } else {
        opt.trigger('change');
      }
        return true;
      });
    dataValidator.addRule('password2', function(val, opt){
      // opt.trigger('change');
      return val===opt.val();
    });
    dataValidator.addRule('effectiveNumber',function(val, opt){
      return !!(val && /^\d+(\.\d{1,2})?$/.test(val));
    });
    dataValidator.addRule('required', function(val, opt){
      if(opt==='password'){
          return !!(val&&!/^(?:\s\w|\u3000)+$/.test(val));
      }else{
          return !!(val&&!/^(?:\s|\u3000)+$/.test(val));
      }
    });
  });
});
